package com.mall.service.order.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsOrderReturnApply;
import com.mall.model.dto.ReturnApplyDto;
import com.mall.model.dto.StatusParam;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.ReturnPage;
import com.mall.service.order.mapper.OmsOrderReturnApplyMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OmsOrderReturnApplyService {

    @Autowired
    private OmsOrderReturnApplyMapper oms;

    public void delete(List<Integer> ids) {
        oms.deleteBatchIds(ids);

    }

    public PageResult page(ReturnApplyDto returnApplyDto) {
        Page<OmsOrderReturnApply> page = new Page<>(returnApplyDto.getPageNum(), returnApplyDto.getPageSize());
        LambdaQueryWrapper<OmsOrderReturnApply> qw = new LambdaQueryWrapper<>();
        qw.like(returnApplyDto.getHandleMan()!=null,OmsOrderReturnApply::getHandleMan,returnApplyDto.getHandleMan());
        qw.like(returnApplyDto.getId()!=null,OmsOrderReturnApply::getId,returnApplyDto.getId());
        qw.eq(returnApplyDto.getStatus()!=null,OmsOrderReturnApply::getStatus,returnApplyDto.getStatus());
        //  qw.eq(OmsOrderReturnApply::getOrderId, returnApplyDto.getId());
        Page<OmsOrderReturnApply> Page = oms.selectPage(page, qw);
        BeanUtils.copyProperties(page,Page);
        PageResult pageResult = new PageResult();
        pageResult.setList(Page.getRecords());
        pageResult.setPageNum(returnApplyDto.getPageNum());
        pageResult.setPageSize(returnApplyDto.getPageSize());
        pageResult.setTotal(Math.toIntExact(Page.getTotal()));
        pageResult.setTotalPage(Math.toIntExact(Page.getCurrent()));

        return pageResult;

    }

    public void update(Integer id, StatusParam statusParam) {
        LambdaQueryWrapper<OmsOrderReturnApply>qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderReturnApply::getId,id);
        OmsOrderReturnApply omsOrderReturnApply = oms.selectOne(qw);
        BeanUtils.copyProperties(statusParam,omsOrderReturnApply);
        omsOrderReturnApply.setStatus(2);
        oms.updateById(omsOrderReturnApply);
//        omsOrderReturnApply1.setCompanyAddressId(Long.valueOf(statusParam.getCompanyAddressId()));
//        omsOrderReturnApply1.setHandleNote(statusParam.getHandleNote());
//        omsOrderReturnApply1.setHandleMan(statusParam.getHandleMan());
//        omsOrderReturnApply1.setOrderId(Long.valueOf(statusParam.getId()));
//        omsOrderReturnApply1.setReceiveMan(statusParam.getReceiveMan());
//        omsOrderReturnApply1.setReceiveNote(statusParam.getReceiveNote());
//        omsOrderReturnApply1.setReturnAmount(statusParam.getReturnAmount());
//        omsOrderReturnApply1.setStatus(statusParam.getStatus());
      //  oms.updateById(omsOrderReturnApply1);

    }

    public OmsOrderReturnApply get(Integer id) {
        OmsOrderReturnApply omsOrderReturnApply = oms.selectById(id);

        return omsOrderReturnApply;
    }
}
